package org.csu.springjpetstore.domain;

import java.math.BigDecimal;

/**
 * 购物车物品  调用 getCartItemTotal() 每次都会计算一次总价
 */
public class CartItem {
    private Item item;
    private int quantity;
    private boolean inStock;
    private BigDecimal cartItemTotal;//购物车某行商品的总价 商品单价*数量

    public CartItem() {
        this.quantity = 0;
        this.inStock = true;
        this.cartItemTotal = BigDecimal.valueOf(0);
    }

    /**
     * description 计算CartItem的总价 (不是购物车的总价,是单行商品的总价格)
     *
     * @author huangzhangyan
     * @date 2022/1/10 15:49
     */
    private void calculateCartItemTotal() {
        if (item != null && item.getListPrice() != null) {
            cartItemTotal = item.getListPrice().multiply(BigDecimal.valueOf(quantity));
        } else {
            cartItemTotal = null;
        }
    }

    /**
     * description 获取CartItem 单行商品的总价格
     *
     * @return BigDecimal
     * @author huangzhangyan
     * @date 2022/1/10 15:49
     */
    public BigDecimal getCartItemTotal() {
        //每次获取cartItemTotal之前都要进行一次计算
        calculateCartItemTotal();
        return cartItemTotal;
    }

    public boolean isInStock() {
        return inStock;
    }

    public void setInStock(boolean inStock) {
        this.inStock = inStock;
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    @Override
    public String toString() {
        return "CartItem{" +
                "item=" + item +
                ", quantity=" + quantity +
                ", inStock=" + inStock +
                ", total=" + getCartItemTotal() +
                '}';
    }


    public boolean getInStock() {
        return inStock;
    }
}
